package fauxman.utilities;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public final class SpriteUtil {
	//Sprite
	private static int SPRITE_UNIT = 18;  //Pixel units to extract in sprite file
	
	private static int PACMAN_ROWS_BEGIN = 0; //Row number, BEGIN is inclusive
	private static int PACMAN_ROWS_END = 2;   //END is exclusive
	private static int PACMAN_COLUMNS_BEGIN = 0;	//column number
	private static int PACMAN_COLUMNS_END = 9;
	
	private static int BONUS_ROWS_BEGIN = 4;
	private static int BONUS_ROWS_END = 5;
	private static int BONUS_COLUMNS_BEGIN = 0;
	private static int BONUS_COLUMNS_END = 6;
	
	private static int PINKY_ROWS_BEGIN = 2;	//Pink gal
	private static int PINKY_ROWS_END = 4;
	private static int PINKY_COLUMNS_BEGIN = 3;
	private static int PINKY_COLUMNS_END = 6;
	
	private static int BLINKY_ROWS_BEGIN = 2;	//Red guy
	private static int BLINKY_ROWS_END = 4;
	private static int BLINKY_COLUMNS_BEGIN = 0;
	private static int BLINKY_COLUMNS_END = 4;
	
	private static int INKY_ROWS_BEGIN = 2;		//Blue guy
	private static int INKY_ROWS_END = 4;
	private static int INKY_COLUMNS_BEGIN = 9;
	private static int INKY_COLUMNS_END = 12;
	
	private static int CLYDE_ROWS_BEGIN = 2;	//Orange guy
	private static int CLYDE_ROWS_END = 4;
	private static int CLYDE_COLUMNS_BEGIN = 6;
	private static int CLYDE_COLUMNS_END = 9;
	
	private static int VULNERABLE_ROWS_BEGIN = 0;
	private static int VULNERABLE_ROWS_END = 2;
	private static int VULNERABLE_COLUMNS_BEGIN = 10;
	private static int VULNERABLE_COLUMNS_END = 12;
	
	private static int EATEN_ROWS_BEGIN = 4;
	private static int EATEN_ROWS_END = 5;
	private static int EATEN_COLUMNS_BEGIN = 7;
	private static int EATEN_COLUMNS_END = 10;
	
	private static ArrayList<BufferedImage> pacmanSprites;
	private static ArrayList<BufferedImage> bonusSprites;
	private static ArrayList<BufferedImage> pinkySprites;
	private static ArrayList<BufferedImage> blinkySprites;
	private static ArrayList<BufferedImage> inkySprites;
	private static ArrayList<BufferedImage> clydeSprites;
	private static ArrayList<BufferedImage> vulnerableSprites;
	private static ArrayList<BufferedImage> eatenSprites;
	
	static{
		createSprites();
	}
	
	public static void createSprites(){
		BufferedImage img;
		pacmanSprites = new ArrayList<BufferedImage>();
		bonusSprites = new ArrayList<BufferedImage>();
		pinkySprites = new ArrayList<BufferedImage>();
		blinkySprites = new ArrayList<BufferedImage>();
		inkySprites = new ArrayList<BufferedImage>();
		clydeSprites = new ArrayList<BufferedImage>();
		vulnerableSprites = new ArrayList<BufferedImage>();
		eatenSprites = new ArrayList<BufferedImage>();
		
		try {
			img = ImageIO.read(new File("../../res/sprites.png"));
			
			createSpritesHelper(img,	PACMAN_ROWS_BEGIN,	PACMAN_ROWS_END,	PACMAN_COLUMNS_BEGIN,	PACMAN_COLUMNS_END,	pacmanSprites);
			createSpritesHelper(img,	BONUS_ROWS_BEGIN,	BONUS_ROWS_END,		BONUS_COLUMNS_BEGIN,	BONUS_COLUMNS_END,	bonusSprites);
			createSpritesHelper(img,	PINKY_ROWS_BEGIN,	PINKY_ROWS_END,		PINKY_COLUMNS_BEGIN,	PINKY_COLUMNS_END,	pinkySprites);
			createSpritesHelper(img,	BLINKY_ROWS_BEGIN,	BLINKY_ROWS_END,	BLINKY_COLUMNS_BEGIN,	BLINKY_COLUMNS_END,	blinkySprites);
			createSpritesHelper(img,	INKY_ROWS_BEGIN,	INKY_ROWS_END,		INKY_COLUMNS_BEGIN,		INKY_COLUMNS_END,	inkySprites);
			createSpritesHelper(img,	CLYDE_ROWS_BEGIN,	CLYDE_ROWS_END,		CLYDE_COLUMNS_BEGIN,	CLYDE_COLUMNS_END,	clydeSprites);
			createSpritesHelper(img,	VULNERABLE_ROWS_BEGIN,	VULNERABLE_ROWS_END,	VULNERABLE_COLUMNS_BEGIN,	VULNERABLE_COLUMNS_END,	vulnerableSprites);
			createSpritesHelper(img,	EATEN_ROWS_BEGIN,	EATEN_ROWS_END,		EATEN_COLUMNS_BEGIN,	EATEN_COLUMNS_END,	eatenSprites);
			
		} catch (IOException e) {
			e.printStackTrace();
			new Exception("Could not load sprites file.");
		}
	}
	
	private static void createSpritesHelper(BufferedImage img, int SPRITE_ROWS_BEGIN, int SPRITE_ROWS_END, int SPRITE_COLUMNS_BEGIN, int SPRITE_COLUMNS_END, ArrayList<BufferedImage> sprites){
		for(int row = SPRITE_ROWS_BEGIN; row < SPRITE_ROWS_END; row++){
			for(int column = SPRITE_COLUMNS_BEGIN; column < SPRITE_COLUMNS_END; column++){
				sprites.add(img.getSubimage(column*SPRITE_UNIT, row*SPRITE_UNIT, SPRITE_UNIT, SPRITE_UNIT));
			}
		}
	}
	
	public static int getSpriteUnit(){
		return SPRITE_UNIT;
	}
	
	public static BufferedImage getPacmanSprite(int index){
		return pacmanSprites.get(index);
	}
	
	public static BufferedImage getBonusSprite(int index){
		return bonusSprites.get(index);
	}
	
	public static BufferedImage getPinkySprite(int index){
		return pinkySprites.get(index);
	}
	
	public static ArrayList<BufferedImage> getPinkySpriteImages(){
		return pinkySprites;
	}
	
	public static BufferedImage getBlinkySprite(int index){
		return blinkySprites.get(index);
	}
	
	public static BufferedImage getInkySprite(int index){
		return inkySprites.get(index);
	}
	
	public static BufferedImage getClydeSprite(int index){
		return clydeSprites.get(index);
	}
	
	public static BufferedImage getVulnerableSprite(int index){
		return vulnerableSprites.get(index);
	}
	
	public static BufferedImage getEatenSprite(int index){
		return eatenSprites.get(index);
	}
	
}
